import RescacheMgr from "../framework/inc/rescachemgr";
import StagerMgr from "../framework/inc/stagemgr";

export default class ViewBase extends THREE.Group {
    constructor(vname, sfile) {
        super();
        this.name = vname.toUpperCase();

        this._raycaster = new THREE.Raycaster();
        this._vec3 = new THREE.Vector3();
        this._ve2_touch = new THREE.Vector2();

        this._res_mgr = RescacheMgr.getInstance();
        this._obj_intouch = undefined;
    }

    _get_mousetouch(posx, posy) {
        var dom = StagerMgr.getInstance().dom;
        var rect = dom.getBoundingClientRect();
        return [(x - rect.left) / rect.width, (y - rect.top) / rect.height];
    }

    onTouch(touchevt, camera) {
        let x = touchevt.changedTouches[0].pageX, y = touchevt.changedTouches[0].pageY;
        this._ve2_touch.set(x / window.innerWidth * 2 - 1, -(y / window.innerHeight * 2) + 1);
        this._raycaster.setFromCamera(this._ve2_touch, camera);

        let intersects = this._raycaster.intersectObjects(this.children, true);
        if (intersects.length > 0) {
            console.log("intersect cnt > 0");
            let obj = intersects[0].object;
            if (obj) {
                if (obj.is_swidget && obj.onTouch) {
                    console.log("ontouch swidget:name=%s", obj.name);
                    obj.onTouch(touchevt.type);
                    this._intouch_objs = obj;
                }
                else {
                    console.log("ontouch obj is not widget:name=%s", obj.name);
                }
                return true;
            }
        
        }
        else{
            let obj = this._intouch_objs;
            if (obj) {
                if (obj.is_swidget && obj.onCancel) {
                    obj.onCancel();
                }    
            }
        }
    }
}